<?php

class yewuheadAction extends CommonAction
{
    public function index()
    {
        $model = new Model();
        $employee = I('session.userid', 0);



        $wherecondition = "p.custid=c.id and  p.employee=" . $employee . " and p.type=t.id and p.isdelete=0 and  p.status=0 and e.id=" . $employee;
        $projectdata = $model->table('ys_project p ,ys_customer  c,ys_projecttype t,ys_employee e')
            ->where($wherecondition)
            ->field('p.id as pid, p.keyword ,p.createtime ,c.*,t.name as tname,e.name')
            ->order('p.id')
            ->select();

          //  echo $model->getLastSql();exit();
        //   dump($projectdata);
        $this->list = $projectdata;
        $this->display();
    }

   /* public function add()
    {
        $projecttype = M("projecttype");
        $this->list = $projecttype->select();
        $this->display();
    }

    public function addproject()
    {

        // dump($_POST);exit();

        $customer = M('customer');
        $customer->company = I('post.company');
        $customer->contact = I('post.contact');
        $customer->telephone = I('post.telephone');
        $customer->qq = I('post.qq');
        $customer->email = I('post.email');
        $customer->address = I('post.address');
        $customer->createtime = I('post.createtime');
        $custid = $customer->add();

        $project = M('project');
        $project->keyword = I('post.keyword');
        $project->employee = I("session.userid"); //$_SESSION['username'];
        $project->type = I('post.type');
        $project->createtime = date("Y-m-d H:i:s");
        $project->content = I('post.content');


        $project->custid = $custid;
        $project->add();
        $this->success("添加成功");

        // $this->error("添加失败");

    }*/

    public function jindu()
    {
        $this->display();
    }

    public function alljindu()
    {
        //当前工程的进度
        $projectid=I("get.id",0);
        if( empty($projectid)) $this->error("参数传递出错");
        $model=new Model();
        $members=$model->table('ys_project p,ys_employee e,ys_customer c')
            ->where(' p.employee=e.id and p.custid=c.id and p.id='.$projectid)
            ->field('p.*,e.name as employeename,c.company as companyname,c.telephone ,c.contact, c.address as address')
            ->order('p.id')
            ->find();

        $projecttype=$this->CacheArray("projecttype");
        $members["projecttype"]=$projecttype[$members["type"]]["name"];


        $this->pro=$members;

        $site=M("site");
        $site_con["projectid"]=$projectid;
        $sitedata=$site->where($site_con)->select();
        $tasks=M("taskinfo");
        foreach($sitedata as $k=>&$v)
        {
            $task_con["site"]=$v["id"];
            $task_con["parentid"]=0;
            $v["task"]=$tasks->where($task_con)->order("`order` asc,id asc")->select();
            //echo $tasks->getLastSql();
        }
        //dump($sitedata);
        $this->jidulist=$sitedata;





//dump($projectinfo);exit();

        $this->display();
    }

    public function  allyewu($status)
    {

       // dump($_SESSION);//exit();

        $employee = I('session.userid', 0);
        $name = I('post.sousuo');

        $isleader=I("session.isleader",0);
        if ($isleader) $employee = I('session.employee');


        $projectmode=M("project");
        $condition["employee"]=array("exp","in($employee)");
        if ($status)
            $condition["status"] = array("gt", "0");
        else
            $condition["status"] = 0;


        if (IS_POST || !empty($name)) {
            $condition["name"] = array("exp", "like '%$name%'");
        }

        $projectdata = $projectmode->where($condition)->select();
        $employdata = $this->CacheArray("employee");
        $companydata=$this->CacheArray("customer");
        $projtype=$this->CacheArray("projecttype");

        foreach($projectdata as $k=>&$v)
        {
            $empdata=$employdata[$v["employee"]];
            $compdata=$companydata[$v["custid"]];
            $v["employeename"]=$empdata["name"];
            $v["companyname"]=$compdata["company"];
            $v["contact"]=$compdata["contact"];
            $v["protype"]=$projtype[$v["type"]]["name"];
        }


        //echo $projectmode->getLastSql();exit();
        $this->list = $projectdata;
        if ($status)
            $this->display();
        else
            $this->display("index");

    }

    public function myyewu()
    {
        $model = new Model();

        $employee = I('session.userid', 0);
        $name = I('post.sousuo');

        $wherecondition = "p.custid=c.id and  p.employee=" . $employee . " and p.type=t.id and p.isdelete=0";
        if (IS_POST || !empty($name)) {
            $wherecondition = $wherecondition . " and c.company like '%{$name}%'";
        }

        $projectdata = $model->table('ys_project p ,ys_customer  c,ys_projecttype t')
            ->where($wherecondition)
            ->field(' p.keyword ,p.createtime ,c.*,t.name as tname')
            ->order('p.id')
            ->select();

        //echo $model->getLastSql();exit();
        $this->list = $projectdata;
        $this->display();

    }

    public function shenhe()
    {
        // $project=M('project');
        $id = I('get.id');
        // echo $id;
        // $arr=$project->find($id);
        // $this->assign('project',$arr);

        $model = new Model();
        $projectdata = $model->table('ys_project p ,ys_customer  c,ys_projecttype t,ys_employee e')
            ->where("p.custid=c.id and p.id=" . $id . " and p.type=t.id ")
            ->field('p.id as pid,p.beian, p.keyword ,p.createtime,p.content ,c.*,t.name as tname,e.name')
            ->find();
//echo $model->getLastSql();
        $this->assign('pro', $projectdata);
        $this->display();
    }

    public function yewudel()
    {

        $project = M('project');
        $id = I('get.id');
        $data = array();
        $data["id"] = $id;
        $data["isdelete"] = 1;
        //$sql="update ys_project set isdelete=1 where id=$id";
        $res = $project->save($data);
        if ($res > 0) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    public function tongguo()
    {

        $action=I("param.action","");
        if ($action == "back") { //退回业务
            $projectid = I("post.id", 0);
            $statusnote = I("post.statusnote", "");
            $data["id"]=$projectid;
            $data["status"] = -1;
            $data["statusnote"] = $statusnote;
            $data["audittime"]=date("Y-m-d H:i:s");

            $project = M("project");
            $url=U("yewuhead/allyewu/",array("status"=>0));
            $this->assign("jumpUrl",$url);

            if ($project->save($data))
                $this->success("退回成功");
            else
                $this->error("退回失败");
            exit();
        }

        $project = M('project');
        $id = I('post.id');
        $data = array();
        $data["id"] = $id;
        $data["status"] = 1;
        $res = $project->save($data);
        if ($res > 0) {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("yewuhead/index"));
            $this->success("审核成功");
        } else {
            $this->error("审核失败");
        }
    }

    public function yewuupdate()
    {
        $projecttype = M('projecttype');
        $this->list = $projecttype->select();

        $project = M('project');
        $id = I('get.id');
        $arr = $project->find($id);
        $this->assign('data', $arr);
        $this->display();
    }

    public function yewuupdatesave()
    {
        $customer = M('customer');
        $cust['id'] = I('post.custid');
        $cust['company'] = I('post.company');
        $cust['contact'] = I('post.contact');
        $cust['telephone'] = I('post.telephone');
        $cou = $customer->save($cust);
        $project = M('project');
        $pro['id'] = I('post.id');
        $pro['type'] = I('post.type');
        $pro['keyword'] = I('post.keyword');
         $content= I('post.content');
        $content=trim($content);
        $content=htmlspecialchars($content);
        $pro['content']=$content;

        $count = $project->save($pro);
        if ($count > 0 or $cou > 0) {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("yewu/index"));
            $this->success("修改成功");
        } else {
            $this->error("修改失败或未修改");
        }

    }

    public  function  _empty()
    {
        $this->error("你访问的方法不存在");
    }

}